package com.jpf.dao;
import java.sql.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.jpf.demo.Goods;
import com.jpf.demo.Order;
import com.jpf.demo.Page;
import com.jpf.dao.DatabaseConnection;
public class OrderDao {
	DatabaseConnection conn = null;
	public OrderDao() {
		super();//调用父类的方法
		conn = new DatabaseConnection();
	}
	/**
	 * 添加方法
	 * @param order
	 * @return
	 */
	public int insert(Order order) {//oid;uid;gid;otime;address;phone;amount;consignee;status;
		String sql = "insert into `order`(oid;uid;gid;otime;address;phone;amount;consignee;status) values(?,?,?,?,?,?,?,?,?)";
		Object[] param = {order.getOid(),order.getUid(),order.getGid(),order.getOtime(),order.getAddress(),order.getPhone(),order.getAmount(),order.getConsignee(),order.getStatus()};
		conn.getConnection();
		int result = conn.executeUpdate(sql, param); //取得返回结果
		conn.close();
		return result;
	}
	public int deleteOrder(Integer oid) {
		String sql = "delete from `order` where oid=?";
		Object[] param = { oid };
		conn.getConnection();
		int result = conn.executeUpdate(sql, param);
		//conn.close();					//Ajax并发请求时会因为连接关闭发生异常 
		return result;
	}
	public Order queryOrder(int oid) {//根据订单编号查询
		Order order = null;
		String sql = "select * from `order` where oid=?";
		Object[] param = { oid };
		conn.getConnection();
		ResultSet rs = conn.executeQuery(sql, param);
		try {
		   if (rs.next()) {
			   order = new Order(rs.getInt("oid"), rs.getInt("uid"), rs.getInt("gid"), rs.getString("otime"),rs.getString("address"), rs.getString("phone"), rs.getDouble("amount"), rs.getString("consignee"), rs.getString("status"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return order;
	}
	public List<Order> queryOrder() {//查询整个订单表
		List<Order> orderList=null;
		orderList = new ArrayList<Order>();
		Order order = null;
		String sql = "select * from `order`";
		Object[] param = null;
		conn.getConnection();
		ResultSet rs = conn.executeQuery(sql, param);
		try {
			while (rs.next()) {
				order = new Order(rs.getInt("oid"), rs.getInt("uid"), rs.getInt("gid"), rs.getString("otime"),rs.getString("address"), rs.getString("phone"), rs.getDouble("amount"), rs.getString("consignee"), rs.getString("status"));
				orderList.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orderList;
	}
	/**
	 * 修改方法
	 * 
	 * @param goods
	 * @return result
	 */
	public int updateOrder(Order order) {
		String sql = "update `order` set status=?";
		Object[] param = { order.getStatus()};
		conn.getConnection();
		int result = conn.executeUpdate(sql, param);
		conn.close();
		return result;
	}
	/**
	 * 分页方法
	 * @param pageSize 每一页的行数
	 * @param pageNow 当前页
	 * @return page
	 */
	public Page<Order> queryPage(int pageSize, int pageNow) {
		Page<Order> page = new Page<Order>();
		page.setPageSize(pageSize);
		page.setPageNow(pageNow);
		String sql = "select count(*) from order";
		conn.getConnection();
		ResultSet rs = conn.executeQuery(sql, null);
		try {
			rs.next();
			Integer rowCount = rs.getInt(1);
			page.setRowCount(rowCount);
			List<Order> pageRow = new ArrayList<Order>();
			String preparedSql = "select * from order limit ?,?";
			Object[] param = { page.getPageSize() *( page.getPageNow() - 1), page.getPageSize() };
			ResultSet res = conn.executeQuery(preparedSql, param);
			while (res.next()) {
				Order order = new Order(rs.getInt("oid"), rs.getInt("uid"), rs.getInt("gid"), rs.getString("otime"),rs.getString("address"), rs.getString("phone"), rs.getDouble("amount"), rs.getString("consignee"), rs.getString("status"));
				pageRow.add(order);
			}
			page.setpageRow(pageRow);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return page;
	}
}
